import {defineStore} from "pinia";
// 导入cookie
import $cookie from 'vue-cookies'

export const definedUser = defineStore(
    {
        id: 'userPinia', //必须唯一
        state: () => { // state中用于定义数据
            return {
                login_user: {
                    username: $cookie.get('username'),
                    icon: $cookie.get('icon'),
                    token: $cookie.get('token')
                }
            }
        },
        actions: { // 用于定义一些对数据修改的方法
            set_user(user) {
                this.login_user = user
                // 存到 cookie
                $cookie.set('token', user.token, '7d')
                $cookie.set('username', user.username, '7d')
                $cookie.set('icon', user.icon, '7d')
            },
            log_out() {
                this.login_user = {
                    username: '',
                    icon: '',
                    token: ''
                }
                $cookie.remove('username')
                $cookie.remove('icon')
                $cookie.remove('token')
            }
        }
    }
)